{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "43cdda6f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 05_calculate_overlap\n",
    "#\n",
    "# created by LuYF-Lemon-love <luyanfeng_nlp@qq.com> on March 5, 2023\n",
    "# updated by LuYF-Lemon-love <luyanfeng_nlp@qq.com> on March 11, 2023\n",
    "#\n",
    "# 该脚本计算了四个个预训练模型 (transE_l1, transE_l2, ComplEx, rotatE) 药物重定位 (Alzheimer's disease) 结果重叠情况.\n",
    "#\n",
    "# 需要的包:\n",
    "#          csv\n",
    "#\n",
    "# 需要的文件:\n",
    "#          ./results/01_transE_l1_top50.csv\n",
    "#          ./results/02_transE_l2_top50.csv\n",
    "#          ./results/03_ComplEx_top100.csv\n",
    "#          ./results/04_rotatE_top50.csv"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a046a1dc",
   "metadata": {},
   "source": [
    "## TransE_l1 top50 结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "df7ba911",
   "metadata": {},
   "outputs": [],
   "source": [
    "import csv\n",
    "\n",
    "transE_l1_top50 = []\n",
    "with open(\"./results/01_transE_l1_top50.csv\", newline='', encoding='utf-8') as csvfile:\n",
    "    reader = csv.DictReader(csvfile, delimiter=',', fieldnames=['rank', 'drug', 'score'])\n",
    "    for row_val in reader:\n",
    "        transE_l1_top50.append(row_val['drug'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "52d0958a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Compound::DB04216',\n",
       " 'Compound::DB06774',\n",
       " 'Compound::DB00783',\n",
       " 'Compound::DB00295',\n",
       " 'Compound::DB09341',\n",
       " 'Compound::DB00143',\n",
       " 'Compound::DB00363',\n",
       " 'Compound::DB04540',\n",
       " 'Compound::DB00907',\n",
       " 'Compound::DB00917',\n",
       " 'Compound::DB00661',\n",
       " 'Compound::DB01026',\n",
       " 'Compound::DB01183',\n",
       " 'Compound::DB01320',\n",
       " 'Compound::DB00252',\n",
       " 'Compound::DB00624',\n",
       " 'Compound::DB01223',\n",
       " 'Compound::DB00477',\n",
       " 'Compound::DB00563']"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "transE_l1_top50"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f032c7c5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "19"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(transE_l1_top50)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0d7e2450",
   "metadata": {},
   "source": [
    "## TransE_l2 top50 结果¶"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "deee01df",
   "metadata": {},
   "outputs": [],
   "source": [
    "import csv\n",
    "\n",
    "transE_l2_top50 = []\n",
    "with open(\"./results/02_transE_l2_top50.csv\", newline='', encoding='utf-8') as csvfile:\n",
    "    reader = csv.DictReader(csvfile, delimiter=',', fieldnames=['rank', 'drug', 'score'])\n",
    "    for row_val in reader:\n",
    "        transE_l2_top50.append(row_val['drug'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "f92cc499",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Compound::DB04540',\n",
       " 'Compound::DB09341',\n",
       " 'Compound::DB00143',\n",
       " 'Compound::DB00515',\n",
       " 'Compound::DB00997',\n",
       " 'Compound::DB00171',\n",
       " 'Compound::DB01229',\n",
       " 'Compound::DB00477',\n",
       " 'Compound::DB00755',\n",
       " 'Compound::DB00502',\n",
       " 'Compound::DB00783',\n",
       " 'Compound::DB00295',\n",
       " 'Compound::DB00661',\n",
       " 'Compound::DB00675',\n",
       " 'Compound::DB00624',\n",
       " 'Compound::DB00363',\n",
       " 'Compound::DB12153',\n",
       " 'Compound::DB01708',\n",
       " 'Compound::DB00541',\n",
       " 'Compound::DB00959',\n",
       " 'Compound::DB00396',\n",
       " 'Compound::DB00907',\n",
       " 'Compound::DB04216',\n",
       " 'Compound::DB00531']"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "transE_l2_top50"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "8d8b870b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "24"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(transE_l2_top50)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "08c5b920",
   "metadata": {},
   "source": [
    "## ComplEx top100 结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "b8d5c818",
   "metadata": {},
   "outputs": [],
   "source": [
    "import csv\n",
    "\n",
    "ComplEx_top100 = []\n",
    "with open(\"./results/03_ComplEx_top100.csv\", newline='', encoding='utf-8') as csvfile:\n",
    "    reader = csv.DictReader(csvfile, delimiter=',', fieldnames=['rank', 'drug', 'score'])\n",
    "    for row_val in reader:\n",
    "        ComplEx_top100.append(row_val['drug'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "fd10d26f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Compound::DB00715',\n",
       " 'Compound::DB00661',\n",
       " 'Compound::DB08860',\n",
       " 'Compound::DB00959',\n",
       " 'Compound::DB01118',\n",
       " 'Compound::DB08868',\n",
       " 'Compound::DB06287',\n",
       " 'Compound::DB00175',\n",
       " 'Compound::DB00563',\n",
       " 'Compound::DB00759',\n",
       " 'Compound::DB00381',\n",
       " 'Compound::DB00860',\n",
       " 'Compound::DB00678',\n",
       " 'Compound::DB00829',\n",
       " 'Compound::DB01016',\n",
       " 'Compound::DB00457',\n",
       " 'Compound::DB00602',\n",
       " 'Compound::DB00876',\n",
       " 'Compound::DB02315',\n",
       " 'Compound::DB01149',\n",
       " 'Compound::DB01307',\n",
       " 'Compound::DB01054',\n",
       " 'Compound::DB00950',\n",
       " 'Compound::DB01115',\n",
       " 'Compound::DB00363',\n",
       " 'Compound::DB04630',\n",
       " 'Compound::DB00530',\n",
       " 'Compound::DB01224',\n",
       " 'Compound::DB01076',\n",
       " 'Compound::DB01004',\n",
       " 'Compound::DB00983',\n",
       " 'Compound::DB01407',\n",
       " 'Compound::DB00343',\n",
       " 'Compound::DB01023']"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ComplEx_top100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "29e9a918",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "34"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(ComplEx_top100)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "65fecf33",
   "metadata": {},
   "source": [
    "## RotatE top50 结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "3db6591f",
   "metadata": {},
   "outputs": [],
   "source": [
    "rotatE_top50 = []\n",
    "rotatE_top50_ranks = []\n",
    "with open(\"./results/04_rotatE_top50.csv\", newline='', encoding='utf-8') as csvfile:\n",
    "    reader = csv.DictReader(csvfile, delimiter=',', fieldnames=['rank', 'drug', 'score'])\n",
    "    for row_val in reader:\n",
    "        rotatE_top50.append(row_val['drug'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "1de4a648",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Compound::DB00143',\n",
       " 'Compound::DB00502',\n",
       " 'Compound::DB06774',\n",
       " 'Compound::DB04216',\n",
       " 'Compound::DB00783',\n",
       " 'Compound::DB09341',\n",
       " 'Compound::DB00822',\n",
       " 'Compound::DB00640',\n",
       " 'Compound::DB01105',\n",
       " 'Compound::DB00715',\n",
       " 'Compound::DB00907',\n",
       " 'Compound::DB01229',\n",
       " 'Compound::DB04540',\n",
       " 'Compound::DB01016',\n",
       " 'Compound::DB02010',\n",
       " 'Compound::DB14681',\n",
       " 'Compound::DB00321']"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rotatE_top50"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "c8a17bb2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "17"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(rotatE_top50)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a443a7f6",
   "metadata": {},
   "source": [
    "## 4 种模型药物重定位结果重叠情况"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "47f8b4c5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Compound::DB00783\n",
      "Compound::DB00624\n",
      "Compound::DB09341\n",
      "Compound::DB00477\n",
      "Compound::DB04216\n",
      "Compound::DB04540\n",
      "Compound::DB00661\n",
      "Compound::DB00143\n",
      "Compound::DB00295\n",
      "Compound::DB00907\n",
      "Compound::DB00363\n",
      "******************************************\n",
      "11\n"
     ]
    }
   ],
   "source": [
    "# TransE_l1 和 TransE_l2 重接结果\n",
    "overlap_1_2 = set(transE_l1_top50).intersection(set(transE_l2_top50))\n",
    "\n",
    "for drug in overlap_1_2:\n",
    "    print(drug)\n",
    "print(\"*\" * 42)\n",
    "print(len(overlap_1_2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "9ff76a86",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Compound::DB00363\n",
      "Compound::DB00661\n",
      "Compound::DB00563\n",
      "******************************************\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "# TransE_l1 和 ComplEx 重接结果\n",
    "overlap_1_3 = set(transE_l1_top50).intersection(set(ComplEx_top100))\n",
    "\n",
    "for drug in overlap_1_3:\n",
    "    print(drug)\n",
    "print(\"*\" * 42)\n",
    "print(len(overlap_1_3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "9dac2b6b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Compound::DB00783\n",
      "Compound::DB09341\n",
      "Compound::DB04216\n",
      "Compound::DB04540\n",
      "Compound::DB00143\n",
      "Compound::DB06774\n",
      "Compound::DB00907\n",
      "******************************************\n",
      "7\n"
     ]
    }
   ],
   "source": [
    "# TransE_l1 和 RotatE 重接结果\n",
    "overlap_1_4 = set(transE_l1_top50).intersection(set(rotatE_top50))\n",
    "\n",
    "for drug in overlap_1_4:\n",
    "    print(drug)\n",
    "print(\"*\" * 42)\n",
    "print(len(overlap_1_4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "0605c589",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Compound::DB00959\n",
      "Compound::DB00363\n",
      "Compound::DB00661\n",
      "******************************************\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "# TransE_l2 和 ComplEx 重接结果\n",
    "overlap_2_3 = set(transE_l2_top50).intersection(set(ComplEx_top100))\n",
    "\n",
    "for drug in overlap_2_3:\n",
    "    print(drug)\n",
    "print(\"*\" * 42)\n",
    "print(len(overlap_2_3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "02062be8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Compound::DB00783\n",
      "Compound::DB09341\n",
      "Compound::DB04216\n",
      "Compound::DB04540\n",
      "Compound::DB00143\n",
      "Compound::DB00907\n",
      "Compound::DB01229\n",
      "Compound::DB00502\n",
      "******************************************\n",
      "8\n"
     ]
    }
   ],
   "source": [
    "# TransE_l2 和 RotatE 重接结果\n",
    "overlap_2_4 = set(transE_l2_top50).intersection(set(rotatE_top50))\n",
    "\n",
    "for drug in overlap_2_4:\n",
    "    print(drug)\n",
    "print(\"*\" * 42)\n",
    "print(len(overlap_2_4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "d620b0c5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Compound::DB00715\n",
      "Compound::DB01016\n",
      "******************************************\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "# ComplEx 和 RotatE 重接结果\n",
    "overlap_3_4 = set(ComplEx_top100).intersection(set(rotatE_top50))\n",
    "\n",
    "for drug in overlap_3_4:\n",
    "    print(drug)\n",
    "print(\"*\" * 42)\n",
    "print(len(overlap_3_4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "699bdb96",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Compound::DB00363\n",
      "Compound::DB00661\n",
      "******************************************\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "# TransE_l1, TransE_l2, ComplEx 重接结果\n",
    "overlap_1_2_3 = set(transE_l1_top50).intersection(set(transE_l2_top50)).intersection(set(ComplEx_top100))\n",
    "\n",
    "for drug in overlap_1_2_3:\n",
    "    print(drug)\n",
    "print(\"*\" * 42)\n",
    "print(len(overlap_1_2_3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "54575271",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Compound::DB00783\n",
      "Compound::DB09341\n",
      "Compound::DB04216\n",
      "Compound::DB04540\n",
      "Compound::DB00143\n",
      "Compound::DB00907\n",
      "******************************************\n",
      "6\n"
     ]
    }
   ],
   "source": [
    "# TransE_l1, TransE_l2, RotatE 重接结果\n",
    "overlap_1_2_4 = set(transE_l1_top50).intersection(set(transE_l2_top50)).intersection(set(rotatE_top50))\n",
    "overlap_1_2_4\n",
    "for drug in overlap_1_2_4:\n",
    "    print(drug)\n",
    "print(\"*\" * 42)\n",
    "print(len(overlap_1_2_4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "1f886152",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "set()"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# TransE_l1, ComplEx, RotatE 重接结果\n",
    "overlap_1_3_4 = set(transE_l1_top50).intersection(set(ComplEx_top100)).intersection(set(rotatE_top50))\n",
    "overlap_1_3_4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "a92e7cc2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "set()"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# TransE_l2, ComplEx, RotatE 重接结果\n",
    "overlap_2_3_4 = set(transE_l2_top50).intersection(set(ComplEx_top100)).intersection(set(rotatE_top50))\n",
    "overlap_2_3_4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "ffdc69ff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "set()"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# TransE_l1, TransE_l2, ComplEx, RotatE 重接结果\n",
    "overlap = set(transE_l1_top50).intersection(set(transE_l2_top50)).intersection(set(ComplEx_top100)).intersection(set(rotatE_top50))\n",
    "overlap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e91a6f6a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
